﻿using Microsoft.Rtc.Collaboration;
using Microsoft.Rtc.Collaboration.ConferenceManagement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MSFT_SAMPLE_ConferencingServiceLibrary
{
    class DataObjects
    {
    }

    public class ConferenceItem
    {
        private string _organizer;
        private string _conferenceId;
        private string _conferenceURL;
        private string _accessLevel;
        private List<List<string>> _participants;
        private string[] _participantsAdmin;
        private string[] _participantsGuests;
        private string _accouncements;
        private string _automaticLeaderAssignment;
        private string _subject;
        private string _description;
        private bool _isPassCodeOptional;
        private bool _phoneAccessEnabled;
        private string _lobbyBypass;
        private string _expiryTime;
        private long _version;
        private string _webUrl;
        private string _lastUpdate;
        private string _phoneAccessId;
        private string _passcode;
        private string _mcu;

        public ConferenceItem()
        {
        }

        public string Mcu
        {
            get
            {
                return this._mcu;
            }
            set
            {
                this._mcu = value;
            }
        }

        public string Passcode
        {
            get
            {
                return this._passcode;
            }
            set
            {
                this._passcode = value;
            }
        }

        public string PhoneAccessId
        {
            get
            {
                return this._phoneAccessId;
            }
            set
            {
                this._phoneAccessId = value;
            }
        }

        public string LastUpdate
        {
            get
            {
                return this._lastUpdate;
            }
            set
            {
                this._lastUpdate = value;
            }
        }

        public string WebUrl
        {
            get
            {
                return this._webUrl;
            }
            set
            {
                this._webUrl = value;
            }
        }

        public long Version
        {
            get
            {
                return this._version;
            }
            set
            {
                this._version = value;
            }
        }

        public bool PhoneAccessEnabled
        {
            get
            {
                return this._phoneAccessEnabled;
            }
            set
            {
                this._phoneAccessEnabled = value;
            }
        }

        public string LobbyBypass
        {
            get
            {
                return this._lobbyBypass;
            }
            set
            {
                this._lobbyBypass = value;
            }
        }

        public bool IsPassCodeOptional
        {
            get
            {
                return this._isPassCodeOptional;
            }
            set
            {
                this._isPassCodeOptional = value;
            }
        }

        public string ExpiryTime
        {
            get
            {
                return this._expiryTime;
            }
            set
            {
                this._expiryTime = value;
            }
        }

        public List<List<string>> Participants
        {
            get
            {
                return this._participants;
            }
            set
            {
                this._participants = value;
            }
        }

        public string Subject
        {
            get
            {
                return this._subject;
            }
            set
            {
                this._subject = value;
            }
        }

        public string Description
        {
            get
            {
                return this._description;
            }
            set
            {
                this._description = value;
            }
        }

        public string Organizer
        {
            get
            {
                return this._organizer;
            }
            set
            {
                this._organizer = value;
            }
        }

        public string AutomaticLeaderAssignment
        {
            get
            {
                return this._automaticLeaderAssignment;
            }
            set
            {
                this._automaticLeaderAssignment = value;
            }
        }

        public string AttendanceAnnouncementsStatus
        {
            get
            {
                return this._accouncements;
            }
            set
            {
                this._accouncements = value;
            }
        }

        public string[] ParticipantsGuests
        {
            get
            {
                return this._participantsGuests;
            }
            set
            {
                this._participantsGuests = value;
            }
        }

        public string[] ParticipantsAdmin
        {
            get
            {
                return this._participantsAdmin;
            }
            set
            {
                this._participantsAdmin = value;
            }
        }

        public string AccessLevel
        {
            get
            {
                return this._accessLevel;
            }
            set
            {
                this._accessLevel = value;
            }
        }

        public string ConferenceId
        {
            get
            {
                return this._conferenceId;
            }
            set
            {
                this._conferenceId = value;
            }
        }

        public string ConferenceUrl
        {
            get
            {
                return this._conferenceURL;
            }
            set
            {
                this._conferenceURL = value;
            }
        }



    }


    public class DataUtilities{
        public static T ParseEnum<T>(string value)
        {
            return (T)Enum.Parse(typeof(T), value, true);
        }

        public static ConferenceItem UCMAConf2SimpleConf(Conference _ucConf)
        {
            ConferenceItem conf = new ConferenceItem();
            conf.AccessLevel = _ucConf.AccessLevel.ToString();
            conf.AttendanceAnnouncementsStatus = _ucConf.AttendanceAnnouncementsStatus.ToString();
            conf.AutomaticLeaderAssignment = _ucConf.AutomaticLeaderAssignment.ToString();
            conf.ConferenceId = _ucConf.ConferenceId;
            conf.ConferenceUrl = _ucConf.ConferenceUri;
            conf.Description = _ucConf.Description;
            conf.ExpiryTime = _ucConf.ExpiryTime.ToString();
            conf.IsPassCodeOptional = _ucConf.IsPasscodeOptional;
            conf.LobbyBypass = _ucConf.LobbyBypass.ToString();
            conf.Organizer = _ucConf.OrganizerUri;
            if (_ucConf.Participants.Count >0)
            {
                conf.Participants = new List<List<string>>();
                foreach (ConferenceParticipantInformation p in _ucConf.Participants)
                {
                    
                    conf.Participants.Add(new List<string>() { p.Uri, p.Role.ToString()});
                }
                 
            }
            conf.PhoneAccessEnabled = true;
            conf.Subject = _ucConf.Subject;
            conf.WebUrl = _ucConf.WebUrl;
            conf.LastUpdate = _ucConf.LastUpdate.ToString();
            conf.PhoneAccessId = _ucConf.PhoneInformation.ConferencePhoneId;
            conf.Passcode = _ucConf.Passcode;
            conf.IsPassCodeOptional = _ucConf.IsPasscodeOptional;
            //TODO:
            //MCU types: chat,data-conf,audio-video,applicationsharing
            //conf.Mcu = _ucConf.Mcus[0].Settings.ToString();

            return conf;
        }

        public static ConferenceScheduleInformation SimpleConf2UCMAConfInfo(ConferenceItem _conf)
        {
            ConferenceScheduleInformation info = new ConferenceScheduleInformation();

           
            info.AccessLevel = DataUtilities.ParseEnum<ConferenceAccessLevel>(_conf.AccessLevel);
            info.AutomaticLeaderAssignment = DataUtilities.ParseEnum<AutomaticLeaderAssignment>(_conf.AutomaticLeaderAssignment);
            info.AttendanceAnnouncementsStatus = DataUtilities.ParseEnum<AttendanceAnnouncementsStatus>(_conf.AttendanceAnnouncementsStatus);
            info.Description = _conf.Description;
            info.ExpiryTime = Convert.ToDateTime(_conf.ExpiryTime);
            info.IsPasscodeOptional = _conf.IsPassCodeOptional;
            info.LobbyBypass = DataUtilities.ParseEnum<LobbyBypass>(_conf.LobbyBypass);
            info.PhoneAccessEnabled = _conf.PhoneAccessEnabled;
            info.Version = _conf.Version;
            info.Subject = _conf.Subject;
            info.Passcode = _conf.Passcode;
            info.IsPasscodeOptional = _conf.IsPassCodeOptional;
            //
            //Add and convert List of strings to participant objects, 0=uri, 1=Role[Leader,Attendee] if everyone isnt admitted
            //
            
            if (_conf.AccessLevel != "Everyone" && _conf.Participants.Count > 0)
            {
                foreach (List<string> p in _conf.Participants)
                {
                   info.Participants.Add( new ConferenceParticipantInformation(p[0], DataUtilities.ParseEnum<ConferencingRole>(p[1])));
                }    
            }
            return info;
        }

    }
    
}